package com.mc.dao.Impl;

import com.mc.bean.Role;
import com.mc.dao.RoleDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zh
 * 角色（权限）数据访问接口
 */
public class RoleDaoImpl extends BaseDao implements RoleDao {
    @Override
    public List<Role> findAll() {
        List<Role> list=new ArrayList<>();
        try {
            //1.注册驱动
            super.regist();
            //2.连接数据库
            super.connection();
            //3.创建sql语句+预编译指令+设置指定参数
            String sql="select * from Role";
            super.getPst(sql);
            //4.得到结果集
            super.getRs();
            while (rs.next()){
                Role role=new Role(rs.getInt(1),rs.getString(2));
                list.add(role);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
        }
        return list;
    }

    @Override
    public Role findRid(int rid) {
        Role role=null;
        try {
            //1.注册驱动
            super.regist();
            //2.连接数据库
            super.connection();
            //3.创建sql语句+预编译指令+设置指定参数
            String sql="select * from role where rid=?";
            super.getPst(sql);
            super.pstmt.setInt(1,rid);
            //4.得到结果集
            super.getRs();
            while (rs.next()){
                role=new Role(rs.getInt(1),rs.getString(2));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
        }
        return role;
    }

    @Override
    public int add(Role role) {
        int i=0;
        try {
            //1.注册驱动
            super.regist();
            //2.连接数据库
            super.connection();
            //3.创建sql语句+预编译指令+设置指定参数
            String sql="insert into role (rname)values(?)";
            super.getPst(sql);
            super.pstmt.setString(1,role.getRname());
            //4.得到结果集
          i=pstmt.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //5.关闭
            close();
        }

        return i;
    }

    @Override
    public int delete(int rid) {
        int i=0;
        try {
            //1.注册驱动
            super.regist();
            //2.连接数据库
            super.connection();
            //3.创建sql语句+预编译指令+设置指定参数
            String sql="delete from role where rid=?";
            super.getPst(sql);
            super.pstmt.setInt(1,rid);
            //4.得到结果集
           i=pstmt.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //5.关闭
            close();
        }

        return i;
    }


    @Override
    public int update(Role role) {
        int i=0;
        try {
            //1.注册驱动
            super.regist();
            //2.连接数据库
            super.connection();
            //3.创建sql语句+预编译指令+设置指定参数
            String sql="update role set rname=? where rid=?";
            super.getPst(sql);
            super.pstmt.setString(1,role.getRname());
            super.pstmt.setInt(2,role.getRid());
            //4.得到结果集
            i=pstmt.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //5.关闭
            close();
        }

        return i;
    }
}
